/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-07-06 15:38:20
 * @LastEditors: 商晓彬
 * @LastEditTime: 2022-01-04 14:39:04
 */
import { LazyLoadEvent } from 'primeng/api';
import { Component, Injector, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { TimeUpdateComponent } from './time-update/time-update.component'
import { CalendarSettingServiceProxy } from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import { MessageService } from '@abp/message/message.service';
import { TimeShowResumeComponent } from './time-show-resume/time-show-resume.component'
@Component({
    selector: 'app-time-manage',
    templateUrl: './time-manage.component.html',
    styleUrls: ['./time-manage.component.less']
})
export class TimeManageComponent extends AppComponentBase {
    @ViewChild('timeUpdate') timeUpdate: TimeUpdateComponent
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('appshowResume') appshowResume: TimeShowResumeComponent;
    calendarName: string = undefined;
    workArrangement = undefined;
    selectRow: any;// 选中的行数据
    rowdata: any;
    title: any;
    tableData: [];
    isUsedOptions = [
        {
            label: '工作',
            value: 1
        }, {
            label: '休息',
            value: 0
        }
    ];
    name = [];
    constructor(
        injector: Injector,
        private _calendarSettingServiceProxy: CalendarSettingServiceProxy,
        private _messageService: MessageService,
    ) {
        super(injector);
    }

    ngOnInit() {
        this.initName();
    }
    getTableList(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        this._calendarSettingServiceProxy.getCalendarSettingList(
            this.calendarName,
            this.workArrangement,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event)
        ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            this.primengTableHelper.hideLoadingIndicator();
        });
    }
    initName() {
        this.name = [];
        this._calendarSettingServiceProxy.getCalendarNameList()
            .pipe(finalize(() => { })).subscribe(result => {
                result.forEach(item => {
                    this.name.push({
                        label: item.label,
                        value: item.label
                    })
                })
            })
    }
    look(row) {
        // //履历
        this.rowdata = row;
        setTimeout(() => {
            this.appshowResume.show()
            this.appshowResume.getTableList()
        }, 20)
    }
    //重置
    reset() {
        this.workArrangement = undefined;
        this.calendarName = undefined;
        this.selectRow = undefined;
    }
    //新增、修改
    addOrUpdate(row?: any) {
        if (row != undefined) {
            this.rowdata = row;
            this.title = '修改日历设置'
        } else {
            this.title = '新增日历设置';
        }
        this.timeUpdate.show();
    }
    delete() {
        this.message.confirm(
            '确认删除这条数据吗',
            (isConfirmed) => {
                if (isConfirmed) {
                    //删除
                    this._calendarSettingServiceProxy.deleteCalendarSetting(
                        this.selectRow
                    ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                        this._messageService.success('删除成功！');
                        this.reset();
                        this.initName();
                        this.getTableList();
                    });
                }
            }
        );

    }
}
